package com.effective.java.taggedclasses;

/**
 * Class representing a figure which can either be a rectangle or a circle.
 * Based on the provided input data the area of the figure is calculated.
 * 
 * @author andreas.landerer
 * 
 */
public class TaggedClassFigure {

	/**
	 * Enumeration containing two values that can be used by the class
	 * {@link TaggedClassFigure}.
	 * 
	 * @author andreas.landerer
	 * 
	 */
	private enum Shape {
		RECTANGLE, CIRCLE
	};

	/**
	 * TAG field determining whether the figure is a circle or a rectangle.
	 */
	private final Shape shape;

	/**
	 * The length of a rectangle.
	 */
	private double length;

	/**
	 * The radius of a circle.
	 */
	private double radius;

	/**
	 * The width of a rectangle.
	 */
	private double width;

	/**
	 * Constructor used in order to initialize a new circle.
	 * 
	 * @param radius
	 *            radius of the circle
	 */
	public TaggedClassFigure(double radius) {

		this.shape = Shape.CIRCLE;
		this.radius = radius;
	}

	/**
	 * Constructor used in order to initialize a new rectangle.
	 * 
	 * @param length
	 *            length of the rectangle
	 * @param width
	 *            width of the rectangle
	 */
	public TaggedClassFigure(double length, double width) {

		this.shape = Shape.RECTANGLE;
		this.length = length;
		this.width = width;
	}

	/**
	 * Calculates the area of the figure.
	 * 
	 * @return area of the figure
	 */
	public double area() {

		switch (this.shape) {
		case RECTANGLE:
			return this.length * this.width;
		case CIRCLE:
			return Math.PI * (this.radius * this.radius);
		default:
			throw new RuntimeException("Area of figure " + this.shape + " can not be calculated!");
		}
	}
}
